package com.cencat.product.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.cencat.common.entity.BaseEntity;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.persistence.*;
import jakarta.validation.constraints.*;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 产品实体类
 * 
 * @author cencat
 * @since 2024-01-01
 */
@Entity
@Table(name = "prd_product", indexes = {
        @Index(name = "idx_product_code", columnList = "product_code"),
        @Index(name = "idx_product_name", columnList = "product_name"),
        @Index(name = "idx_category_id", columnList = "category_id"),
        @Index(name = "idx_brand_id", columnList = "brand_id"),
        @Index(name = "idx_merchant_id", columnList = "merchant_id"),
        @Index(name = "idx_status", columnList = "status"),
        @Index(name = "idx_tenant_id", columnList = "tenant_id")
})
@Data
@EqualsAndHashCode(callSuper = false)
@TableName("prd_product")
@Schema(description = "产品实体")
public class Product extends BaseEntity {

    /**
     * 产品ID
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @TableId(type = IdType.AUTO)
    @Schema(description = "产品ID")
    private Long id;

    /**
     * 产品编码
     */
    @Column(name = "product_code", nullable = false, length = 50, unique = true)
    @NotBlank(message = "产品编码不能为空")
    @Size(max = 50, message = "产品编码长度不能超过50个字符")
    @Schema(description = "产品编码")
    private String productCode;

    /**
     * 产品名称
     */
    @Column(name = "product_name", nullable = false, length = 200)
    @NotBlank(message = "产品名称不能为空")
    @Size(max = 200, message = "产品名称长度不能超过200个字符")
    @Schema(description = "产品名称")
    private String productName;

    /**
     * 产品简称
     */
    @Column(name = "short_name", length = 100)
    @Size(max = 100, message = "产品简称长度不能超过100个字符")
    @Schema(description = "产品简称")
    private String shortName;

    /**
     * 产品描述
     */
    @Column(name = "description", columnDefinition = "TEXT")
    @Schema(description = "产品描述")
    private String description;

    /**
     * 产品详情
     */
    @Column(name = "detail", columnDefinition = "LONGTEXT")
    @Schema(description = "产品详情")
    private String detail;

    /**
     * 产品分类ID
     */
    @Column(name = "category_id", nullable = false)
    @NotNull(message = "产品分类不能为空")
    @Schema(description = "产品分类ID")
    private Long categoryId;

    /**
     * 品牌ID
     */
    @Column(name = "brand_id")
    @Schema(description = "品牌ID")
    private Long brandId;

    /**
     * 商家ID
     */
    @Column(name = "merchant_id", nullable = false)
    @NotNull(message = "商家ID不能为空")
    @Schema(description = "商家ID")
    private Long merchantId;

    /**
     * 产品类型
     */
    @Column(name = "product_type", nullable = false, length = 20)
    @Enumerated(EnumType.STRING)
    @NotNull(message = "产品类型不能为空")
    @Schema(description = "产品类型")
    private ProductType productType;

    /**
     * 产品状态
     */
    @Column(name = "status", nullable = false, length = 20)
    @Enumerated(EnumType.STRING)
    @NotNull(message = "产品状态不能为空")
    @Schema(description = "产品状态")
    private ProductStatus status;

    /**
     * 销售价格
     */
    @Column(name = "sale_price", precision = 10, scale = 2)
    @DecimalMin(value = "0.00", message = "销售价格不能小于0")
    @Schema(description = "销售价格")
    private BigDecimal salePrice;

    /**
     * 市场价格
     */
    @Column(name = "market_price", precision = 10, scale = 2)
    @DecimalMin(value = "0.00", message = "市场价格不能小于0")
    @Schema(description = "市场价格")
    private BigDecimal marketPrice;

    /**
     * 成本价格
     */
    @Column(name = "cost_price", precision = 10, scale = 2)
    @DecimalMin(value = "0.00", message = "成本价格不能小于0")
    @Schema(description = "成本价格")
    private BigDecimal costPrice;

    /**
     * 库存数量
     */
    @Column(name = "stock_quantity", nullable = false)
    @Min(value = 0, message = "库存数量不能小于0")
    @Schema(description = "库存数量")
    private Integer stockQuantity = 0;

    /**
     * 预警库存
     */
    @Column(name = "warning_stock")
    @Min(value = 0, message = "预警库存不能小于0")
    @Schema(description = "预警库存")
    private Integer warningStock = 0;

    /**
     * 最小库存
     */
    @Column(name = "min_stock")
    @Min(value = 0, message = "最小库存不能小于0")
    @Schema(description = "最小库存")
    private Integer minStock = 0;

    /**
     * 最大库存
     */
    @Column(name = "max_stock")
    @Min(value = 0, message = "最大库存不能小于0")
    @Schema(description = "最大库存")
    private Integer maxStock = 0;

    /**
     * 产品重量（克）
     */
    @Column(name = "weight")
    @DecimalMin(value = "0.00", message = "产品重量不能小于0")
    @Schema(description = "产品重量（克）")
    private BigDecimal weight;

    /**
     * 产品体积（立方厘米）
     */
    @Column(name = "volume")
    @DecimalMin(value = "0.00", message = "产品体积不能小于0")
    @Schema(description = "产品体积（立方厘米）")
    private BigDecimal volume;

    /**
     * 产品尺寸
     */
    @Column(name = "dimensions", length = 100)
    @Size(max = 100, message = "产品尺寸长度不能超过100个字符")
    @Schema(description = "产品尺寸")
    private String dimensions;

    /**
     * 产品颜色
     */
    @Column(name = "color", length = 50)
    @Size(max = 50, message = "产品颜色长度不能超过50个字符")
    @Schema(description = "产品颜色")
    private String color;

    /**
     * 产品材质
     */
    @Column(name = "material", length = 100)
    @Size(max = 100, message = "产品材质长度不能超过100个字符")
    @Schema(description = "产品材质")
    private String material;

    /**
     * 产品产地
     */
    @Column(name = "origin", length = 100)
    @Size(max = 100, message = "产品产地长度不能超过100个字符")
    @Schema(description = "产品产地")
    private String origin;

    /**
     * 保质期（天）
     */
    @Column(name = "shelf_life")
    @Min(value = 0, message = "保质期不能小于0")
    @Schema(description = "保质期（天）")
    private Integer shelfLife;

    /**
     * 生产日期
     */
    @Column(name = "production_date")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Schema(description = "生产日期")
    private LocalDateTime productionDate;

    /**
     * 过期日期
     */
    @Column(name = "expiry_date")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Schema(description = "过期日期")
    private LocalDateTime expiryDate;

    /**
     * 产品图片
     */
    @Column(name = "images", columnDefinition = "TEXT")
    @Schema(description = "产品图片（JSON数组）")
    private String images;

    /**
     * 产品视频
     */
    @Column(name = "videos", columnDefinition = "TEXT")
    @Schema(description = "产品视频（JSON数组）")
    private String videos;

    /**
     * 产品标签
     */
    @Column(name = "tags", length = 500)
    @Size(max = 500, message = "产品标签长度不能超过500个字符")
    @Schema(description = "产品标签（逗号分隔）")
    private String tags;

    /**
     * 产品关键词
     */
    @Column(name = "keywords", length = 500)
    @Size(max = 500, message = "产品关键词长度不能超过500个字符")
    @Schema(description = "产品关键词（逗号分隔）")
    private String keywords;

    /**
     * SEO标题
     */
    @Column(name = "seo_title", length = 200)
    @Size(max = 200, message = "SEO标题长度不能超过200个字符")
    @Schema(description = "SEO标题")
    private String seoTitle;

    /**
     * SEO描述
     */
    @Column(name = "seo_description", length = 500)
    @Size(max = 500, message = "SEO描述长度不能超过500个字符")
    @Schema(description = "SEO描述")
    private String seoDescription;

    /**
     * SEO关键词
     */
    @Column(name = "seo_keywords", length = 500)
    @Size(max = 500, message = "SEO关键词长度不能超过500个字符")
    @Schema(description = "SEO关键词")
    private String seoKeywords;

    /**
     * 是否推荐
     */
    @Column(name = "is_recommended", nullable = false)
    @Schema(description = "是否推荐")
    private Boolean isRecommended = false;

    /**
     * 是否热销
     */
    @Column(name = "is_hot", nullable = false)
    @Schema(description = "是否热销")
    private Boolean isHot = false;

    /**
     * 是否新品
     */
    @Column(name = "is_new", nullable = false)
    @Schema(description = "是否新品")
    private Boolean isNew = false;

    /**
     * 是否特价
     */
    @Column(name = "is_special", nullable = false)
    @Schema(description = "是否特价")
    private Boolean isSpecial = false;

    /**
     * 销售数量
     */
    @Column(name = "sales_count", nullable = false)
    @Min(value = 0, message = "销售数量不能小于0")
    @Schema(description = "销售数量")
    private Integer salesCount = 0;

    /**
     * 浏览数量
     */
    @Column(name = "view_count", nullable = false)
    @Min(value = 0, message = "浏览数量不能小于0")
    @Schema(description = "浏览数量")
    private Integer viewCount = 0;

    /**
     * 收藏数量
     */
    @Column(name = "favorite_count", nullable = false)
    @Min(value = 0, message = "收藏数量不能小于0")
    @Schema(description = "收藏数量")
    private Integer favoriteCount = 0;

    /**
     * 评论数量
     */
    @Column(name = "comment_count", nullable = false)
    @Min(value = 0, message = "评论数量不能小于0")
    @Schema(description = "评论数量")
    private Integer commentCount = 0;

    /**
     * 评分
     */
    @Column(name = "rating", precision = 3, scale = 2)
    @DecimalMin(value = "0.00", message = "评分不能小于0")
    @DecimalMax(value = "5.00", message = "评分不能大于5")
    @Schema(description = "评分")
    private BigDecimal rating = BigDecimal.ZERO;

    /**
     * 排序号
     */
    @Column(name = "sort_order", nullable = false)
    @Min(value = 0, message = "排序号不能小于0")
    @Schema(description = "排序号")
    private Integer sortOrder = 0;

    /**
     * 上架时间
     */
    @Column(name = "shelf_time")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Schema(description = "上架时间")
    private LocalDateTime shelfTime;

    /**
     * 下架时间
     */
    @Column(name = "off_shelf_time")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Schema(description = "下架时间")
    private LocalDateTime offShelfTime;

    /**
     * 审核状态
     */
    @Column(name = "audit_status", nullable = false, length = 20)
    @Enumerated(EnumType.STRING)
    @NotNull(message = "审核状态不能为空")
    @Schema(description = "审核状态")
    private AuditStatus auditStatus = AuditStatus.PENDING;

    /**
     * 审核时间
     */
    @Column(name = "audit_time")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Schema(description = "审核时间")
    private LocalDateTime auditTime;

    /**
     * 审核人ID
     */
    @Column(name = "auditor_id")
    @Schema(description = "审核人ID")
    private Long auditorId;

    /**
     * 审核备注
     */
    @Column(name = "audit_remark", length = 500)
    @Size(max = 500, message = "审核备注长度不能超过500个字符")
    @Schema(description = "审核备注")
    private String auditRemark;

    /**
     * 扩展属性
     */
    @Column(name = "extra_attributes", columnDefinition = "JSON")
    @Schema(description = "扩展属性（JSON格式）")
    private String extraAttributes;

    /**
     * 备注
     */
    @Column(name = "remark", length = 500)
    @Size(max = 500, message = "备注长度不能超过500个字符")
    @Schema(description = "备注")
    private String remark;

    /**
     * 产品类型枚举
     */
    public enum ProductType {
        PHYSICAL("实物产品"),
        VIRTUAL("虚拟产品"),
        SERVICE("服务产品"),
        DIGITAL("数字产品"),
        COMBO("组合产品");

        private final String description;

        ProductType(String description) {
            this.description = description;
        }

        public String getDescription() {
            return description;
        }
    }

    /**
     * 产品状态枚举
     */
    public enum ProductStatus {
        DRAFT("草稿"),
        ON_SHELF("上架"),
        OFF_SHELF("下架"),
        SOLD_OUT("售罄"),
        DISCONTINUED("停产");

        private final String description;

        ProductStatus(String description) {
            this.description = description;
        }

        public String getDescription() {
            return description;
        }
    }

    /**
     * 审核状态枚举
     */
    public enum AuditStatus {
        PENDING("待审核"),
        APPROVED("审核通过"),
        REJECTED("审核拒绝");

        private final String description;

        AuditStatus(String description) {
            this.description = description;
        }

        public String getDescription() {
            return description;
        }
    }
}